package cn.com.chec.en.controller.front;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.dao.TagInfoDao;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.domain.TagInfo;
import cn.com.chec.en.model.dto.NewsDTO;
import cn.com.chec.en.util.ContactUtils;
import cn.com.chec.en.util.PageBean;

@Controller
public class TagInfoFrontController {
	protected static final Logger logger = Logger
			.getLogger(TagInfoFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private TagInfoDao tagInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping(value = { "/tag/select" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView showSelect(
			@RequestParam(value = "tagWords", required = false) String tagWords,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/channelInfo/selectTagWords");
		tagWords = StringUtils.isNotBlank(tagWords) ? tagWords : "";
		map.put("tagWords", tagWords);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/tag/{tag}" })
	public ModelAndView tagNews(@PathVariable("tag") String tag,
			@RequestParam(value = "curPage", required = false) String curPage,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("tag");

		map.put("pageLinkMap", this.pageLinkDao.getAllAsMap());

		map.putAll(ContactUtils.getContactMap());

		tag = (StringUtils.isNotBlank(tag)) && (tag.endsWith(",")) ? tag : ",";
		String tagIdStr = tag.replaceAll(",", "");
		int tagId = 0;
		if ((StringUtils.isNotBlank(tagIdStr)) && (tagIdStr.matches("\\d+"))) {
			tagId = Integer.valueOf(tagIdStr).intValue();
		}
		TagInfo tagInfo = this.tagInfoDao.getTagById(tagId);

		PageBean pageBean = new PageBean();
		int curPageInt = (StringUtils.isNotBlank(curPage))
				&& (curPage.matches("\\d+")) ? Integer.parseInt(curPage) : 1;
		pageBean.setCurPage(curPageInt);
		QueryNewsObject info = new QueryNewsObject();
		info.setLanguageId(1);
		info.setTag(tag);
		info.setStart(pageBean.getStart());
		info.setNum(pageBean.getRowsPerPage());
		List<NewsDTO> newsInfoList = new ArrayList<NewsDTO>();
		newsInfoList = this.newsInfoDao.getNewsListByQueryPChannelIdDistinct(
				info, 0);
		pageBean.setMaxRowCount(this.newsInfoDao
				.getNewsCountByQueryPChannelIdDistinct(info, 0));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("tagInfo", tagInfo);
		map.put("newsInfoList", newsInfoList);
		map.put("pageBean", pageBean);
		map.put("tag", tag);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}